<html>
    <head>
        <title>Itowns - 3D Tiles on 2.5D map example</title>

        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="css/example.css">
        <link rel="stylesheet" type="text/css" href="css/LoadingScreen.css">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js"></script>
    </head>
    <body>
        <div id="viewerDiv" class="viewer"></div>
        <script src="../dist/itowns.js"></script>
        <script src="js/GUI/LoadingScreen.js"></script>
        <script src="../dist/debug.js"></script>
        <script src="js/GUI/GuiTools.js"></script>
        <script src="js/3dTilesHelper.js"></script>
        <script type="text/javascript">
            // Define crs projection that we will use (taken from https://epsg.io/3946, Proj4js section)
            itowns.proj4.defs('EPSG:3946', '+proj=lcc +lat_1=45.25 +lat_2=46.75 +lat_0=46 +lon_0=3 +x_0=1700000 +y_0=5200000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');

            // Define geographic extent: CRS, min/max X, min/max Y
            var extent = new itowns.Extent( 'EPSG:3946',
                1837816.94334, 1847692.32501,
                5170036.4587, 5178412.82698);

            // `viewerDiv` will contain iTowns' rendering area (`<canvas>`)
            var viewerDiv = document.getElementById('viewerDiv');

            // Instanciate PlanarView*
            var cameraCoord = new itowns.Coordinates('EPSG:3946', 1841980,
                5175682, 3000)
            var view = new itowns.PlanarView(viewerDiv, extent, { placement: {
                coord: cameraCoord, heading: 30, range: 4000, tilt: 30 } });

            setupLoadingScreen(viewerDiv, view);

            // Add a WMS imagery source
            var wmsImagerySource = new itowns.WMSSource({
                extent: extent,
                name: 'ortho_latest',
                url: 'https://imagerie.data.grandlyon.com/wms/grandlyon',
                version: '1.3.0',
                crs: 'EPSG:3946',
                format: 'image/jpeg',
            });

            // Add a WMS imagery layer
            var wmsImageryLayer = new itowns.ColorLayer('wms_imagery', {
                updateStrategy: {
                    type: itowns.STRATEGY_DICHOTOMY,
                    options: {},
                },
                source: wmsImagerySource,
            });

            view.addLayer(wmsImageryLayer);

            // Add a WMS elevation source
            var wmsElevationSource = new itowns.WMSSource({
                extent: extent,
                url: 'https://imagerie.data.grandlyon.com/wms/grandlyon',
                name: 'MNT2018_Altitude_2m',
                crs: 'EPSG:3946',
                width: 256,
                format: 'image/jpeg',
            });

            // Add a WMS elevation layer
            var wmsElevationLayer = new itowns.ElevationLayer('wms_elevation', {
                useColorTextureElevation: true,
                colorTextureElevationMinZ: 144,
                colorTextureElevationMaxZ: 622,
                source: wmsElevationSource,
            });

            view.addLayer(wmsElevationLayer);

            // Add 3D Tiles layer
            // This 3D Tiles tileset uses the draco compression that is an
            // extension of gltf. We need to enable it.
            itowns.enableDracoLoader('./libs/draco/');

            var $3dTilesLayer = new itowns.C3DTilesLayer(
                '3d-tiles-layer-building', {
                    name: 'Lyon-2015-building',
                    source: new itowns.C3DTilesSource({
                        url: 'https://raw.githubusercontent.com/iTowns/iTowns2-sample-data/master/' +
                            '3DTiles/lyon_1_3946_textured_draco/tileset.json',
                    }),
                }, view);

            // Lights
            var dirLight = new itowns.THREE.DirectionalLight(0xffffff, 1);
            dirLight.position.set(-0.9, 0.3, 1);
            dirLight.updateMatrixWorld();
            view.scene.add( dirLight );

            var ambLight = new itowns.THREE.AmbientLight(0xffffff, 0.2);
            view.scene.add( ambLight );

            // Pick 3D Tiles features when hovering them with the mouse and
            // display their semantic information in an html div
            var pickingArgs = {};
            pickingArgs.htmlDiv = document.getElementById('featureInfo');
            pickingArgs.view = view;
            pickingArgs.layer = $3dTilesLayer;
            itowns.View.prototype.addLayer.call(view, $3dTilesLayer);

            // Request redraw
            view.notifyChange();

            // Add a debug UI
            var menu = new GuiTools('menuDiv', view);
            var d = new debug.Debug(view, menu.gui);
            debug.createTileDebugUI(menu.gui, view, view.tileLayer, d);
            debug.create3dTilesDebugUI(menu.gui, view, $3dTilesLayer);
        </script>
    </body>
</html>
